from helper import *
import textwrap

import wx,re
import wx.richtext as rt

class DisplayFrame(wx.MiniFrame):
	def __init__(self, parent, title, pos, size):
		wx.Frame.__init__(self, parent, -1, title, pos=pos, size=size, style=wx.NO_BORDER|wx.STAY_ON_TOP)#|wx.TRANSPARENT_WINDOW|wx.CLIP_CHILDREN)
		
		#-------------------------------------------------------------------------
		# Panel
		#-------------------------------------------------------------------------
		panel = wx.ScrolledWindow(self, size = size) #, style=wx.TRANSPARENT_WINDOW|wx.CLIP_CHILDREN)
		#panel.EnableScrolling(0, 1)
		#panel.EnableScrolling(0, 1)
		#panel.SetScrollbars(20, 20, 50, 50)
		border = 4
		new_size = (size[0]-border * 2, size[1]-border * 2 - 4)
		#note_text = rt.RichTextCtrl(panel, -1, size=new_size, style=wx.TE_MULTILINE|wx.NO_BORDER|rt.RE_READONLY)#|wx.TE_NO_VSCROLL)
		note_text = wx.StaticText(panel, -1, size=new_size, style=wx.ST_NO_AUTORESIZE)#|rt.RE_READONLY)#|wx.TE_NO_VSCROLL)

		self.note_text = note_text
		os = wx.GetOsDescription()		  
		if re.search('Windows', os, re.I):
			note_text.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL))
		#self.set_auto_wrap()

		#note_text.SetScrollBars(20, 20, 50, 50)

		#-------------------------------------------------------------------------
		# Binding
		#-------------------------------------------------------------------------
		#note_text.Bind(wx.EVT_CHAR, self.OnChar)
		
		
		#-------------------------------------------------------------------------
		# Layout
		#-------------------------------------------------------------------------
		sizer = wx.BoxSizer(wx.VERTICAL)
		sizer.Add(note_text, 0, wx.EXPAND|wx.ALL, border)
		panel.SetSizer(sizer)
		panel.Layout()
		self.panel = panel
		self.length = size[0]
		self.text = ''
		self.height = self.panel.GetVirtualSizeTuple()[1]
		self.pages = []
		self.page_index = 0

	def update_note(self, note):
		self.note = note
		if self.text == note['content']:
			return
		self.text = note['content']
		app = wx.GetApp()

		only_body = app.note.get_body(note).strip()

		self.fill_pages(only_body)

		self.show_page(0)

	def fill_pages(self, text):
		note_text = self.note_text
		char_height = note_text.GetCharHeight() + 2
		height = note_text.GetClientSize()[1]
		max_lines = height/char_height

		lines = self.process_text(text)

		max_pages = len(lines)/max_lines + 1
		pages = []
		for i in range(max_pages):
			start_index = i * max_lines
			end_index = (i+1) * max_lines
			page_lines = lines[start_index:end_index]
			pages.append('\n'.join(page_lines))
			
		self.pages = pages

	def show_page(self, page):
		self.page_index = page
		self.note_text.SetLabel(self.pages[page])

	def process_text(self, text):
		note_text = self.note_text

		width = note_text.GetClientSize()[0]	
		max_len = width / note_text.GetCharWidth()

		lines = text.split('\n')

		lines = ['\n'.join( textwrap.wrap(line, max_len)) for line in lines ]

		return lines

	def set_auto_wrap(self):
		max_width = self.note_text.GetVirtualSizeTuple()[0]
		self.note_text.Wrap(max_width)


	def set_note(self,note):
		self.note = note

	def set_colors(self, color_dict):
		self.panel.SetBackgroundColour(get_fake_color('background'))
		self.note_text.SetBackgroundColour(get_fake_color('background'))
		self.note_text.SetForegroundColour(get_color('foreground'))
		#self.note_text.BeginTextColour(get_color('foreground'))


	def next_page(self):
		if self.page_index >= len(self.pages) - 1:
			return
		next_index = self.page_index + 1
		self.show_page(next_index)


	def previous_page(self):
		if self.page_index <= 0:
			return
		next_index = self.page_index - 1
		self.show_page(next_index)
